ABC 116 D - Various Sushi
貪欲
code: abc116_d.js
class Main {
solve() {
const nums = input.nextIntArr();
const table = input.nextIntRange(nums0); const score = [];
table.sort((a,b) => a1 - b1).reverse(); const s = _.first(table, k);
const kinds = {};
s.forEach(e => kinds[e0] = (kinds[e0] ? kinds[e0] + 1: 1)); let kind = Object.keys(kinds).length;
scorekind = _.sum(s.map(s => s1)); const heap = new BinaryHeap();
s.forEach(e => {
}
});
let cur = k;
while (heap.length > 0 && cur < table.length) {
const target = heap.pop();
if (kinds[target0] >= 2) { do {
++cur;
if (kinds[next0] === undefined) { ++kind;
break;
}
} while(cur < table.length);
}
}
const scores = score.map((e,i) => e ? (e + i * i) : 0);
console.log(_.max(scores));
}
}
値をいれた後でソートを行うことでheapをarrに置き換え可能
配列を用いた解答
code: abc116_d2.js
class Main {
solve() {
const nums = input.nextIntArr();
const table = input.nextIntRange(nums0); const score = [];
table.sort((a,b) => -(a1 - b1)); const s = _.first(table, k);
const kinds = {};
s.forEach(e => kinds[e0] = (kinds[e0] ? kinds[e0] + 1: 1)); let kind = Object.keys(kinds).length;
scorekind = _.sum(s.map(s => s1)); const arr = [];
s.forEach(e => {
arr.push(e);
}
});
arr.sort((a,b) => -(a1 - b1)); let cur = k;
while (arr.length > 0 && cur < table.length) {
const target = arr.pop();
if (kinds[target0] >= 2) { do {
++cur;
if (kinds[next0] === undefined) { ++kind;
break;
}
} while(cur < table.length);
}
}
const scores = score.map((e,i) => e ? (e + i * i) : 0);
console.log(_.max(scores));
}
}